//
//  HZJTabBarButton.swift
//  HZJTab
//
//  Created by HZJ on 2017/4/7.
//  Copyright © 2017年 HZJ. All rights reserved.
//

import UIKit

/**
 需要用init(frame: CGRect)方法创建
 需用设置theTitle的值得方法，以实现语言国际化
 */
class HZJTabBarButton: UIButton {
    ///是否是正常的按钮（图片与文字的位置，点击效果以及小红点的存在）
    var isNormal:Bool = false
    //按钮上的标题
    var theTitle:String = "" {
        didSet{
            self.setTitle(Localized(theTitle), for: UIControlState())
        }
    }
    //刷新按钮上的标题
    func reloadTheTitle(){
        self.setTitle(Localized(theTitle), for: UIControlState())
    }
    
    //作标记用，等同于tag的效果
    var index:NSInteger = 0
    
    //图片所占的最大高度比例（在layoutSubviews中）
    fileprivate var imageScale:CGFloat = 0.6
    /**
    是否有点击效果（想得我好蛋疼）
    */
    override var isHighlighted:Bool {
        didSet{
            if self.isNormal == false {
                super.isHighlighted = false
            }
        }
    }
    /**
     tab上的小红点的值
     */
    var badgeValue: String?{
        didSet{
            if badgeValue != nil {
                redLab.isHidden = false
                redLab.text = badgeValue
                
            }else{
                redLab.isHidden = true
                redLab.text = ""
            }
        }
    }
    /**
     tab上的小红点，默认隐藏
     */
    fileprivate  lazy var redLab:UILabel = {
        let lab = UILabel()
        lab.isHidden = true
        lab.backgroundColor = UIColor.red
        lab.textAlignment = .center
        lab.textColor = UIColor.white
        lab.font = UIFont.systemFont(ofSize: 8)
        return lab
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(redLab)
        self.setTitleColor(UIColor.black, for: UIControlState())
        self.setTitleColor(UIColor.red, for: UIControlState.selected)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if isNormal == false {
            let width = self.frame.width
            let height = self.frame.height
            let btnW_Max = width >= height * imageScale ? height * imageScale : width
            let btnW = btnW_Max * 0.75
            self.imageView?.frame  = CGRect(x: 0, y: 0, width: btnW, height: btnW)
            self.imageView?.center = CGPoint(x: width * 0.5, y: height * imageScale - btnW * 0.5)
            self.titleLabel?.frame = CGRect(x: 0, y: 0, width: width, height: height * (1 - imageScale))
            self.titleLabel?.center = CGPoint(x: width * 0.5, y: height * 0.5 * (1 + imageScale))
            self.titleLabel?.textAlignment = .center
            self.titleLabel?.font = UIFont.systemFont(ofSize: height * (1 - imageScale) * 0.5)
            
            let redLabWidth:CGFloat = self.badgeValue == "" ? 10 : 16
            redLab.frame =  CGRect(x: 0, y: 0, width: redLabWidth, height: redLabWidth)
            redLab.center = CGPoint(x: self.imageView!.frame.maxX, y: 2 + redLabWidth * 0.5)
            redLab.layer.cornerRadius = redLabWidth * 0.5
            redLab.layer.masksToBounds = true
        }
    }
    
}
